/*
Simple Forum Auto Poster.
Copyright (C) 2011 Gianni Van Hoecke.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package domain.forumdata;

import domain.DomainController;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.logging.Level;

/**
 *
 * @author Gianni Van Hoecke <gianni.vh@gmail.com>
 * Contains the forums.
 */
public class Forums implements Serializable
{
    private ArrayList<Forum> forums;
    private boolean viewUI;

    /**
     * Default constructor. Initializes an empty list of forums.
     */
    public Forums()
    {
        this.forums = new ArrayList<Forum>();
        this.viewUI = true;
    }

    /**
     * Creates a new forum. Triggers a save of all forums.
     * @param forum
     */
    public void addForum(Forum forum)
    {
        this.forums.add(forum);
        DomainController.getInstance().getLogger().log(Level.INFO, "Created new forum \"{0}\"", forum.getForumName());
        DomainController.getInstance().loadForumsUI();
        DomainController.getInstance().saveForums();
    }

    /**
     * Removes a forum. Triggers a save of all forums.
     * @param forum The forum object to remove.
     */
    public void removeForum(Forum forum)
    {
        this.forums.remove(forum);
        DomainController.getInstance().getLogger().log(Level.INFO, "Removed forum \"{0}\"", forum.getForumName());
        DomainController.getInstance().loadForumsUI();
        DomainController.getInstance().saveForums();
    }

    /**
     * @return the list of forums.
     */
    public ArrayList<Forum> getForums()
    {
        return forums;
    }

    /**
     * Checks whether a forum with the specified name already exists or not.
     * @param forumName The name to look for.
     * @return True if a forum with the given name already exists, false otherwise.
     */
    public boolean isInList(String forumName)
    {
        for(Forum f : this.getForums())
        {
            if(f.getForumName().equals(forumName))
                return true;
        }
        return false;
    }

    /**
     * @param selectedForum The forum to retrieve.
     * @return The forum object.
     */
    public Forum getForumByName(String selectedForum)
    {
        for(Forum f : this.getForums())
        {
            if(f.getForumName().equals(selectedForum))
                return f;
        }
        return null;
    }

    /**
     * @return Wheather the forum UI must be shown or not.
     */
    public boolean isViewUI()
    {
        return viewUI;
    }

    /**
     * Sets the value to show the forum UI or not.
     * @param viewUI True to show the forum UI to the user, false otherwise.
     */
    public void setViewUI(boolean viewUI)
    {
        this.viewUI = viewUI;
    }
}